﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PLC
{
    public partial class SetForm : Form
    {
        MySqlConnection conn;                          //远程数据库连接
        public static string MysqlCon { get; private set; } = 际海环境软件.Properties.Settings.Default.MySqlCon;
        public SetForm()
        {
            InitializeComponent();
        }

        private void SetForm_Load(object sender, EventArgs e)
        {

            anLtxB.Text = Form1.Refval[0].ToString();
            anHtxB.Text = Form1.Refval[1].ToString();
            tempLtxB.Text = Form1.Refval[2].ToString();
            tempHtxB.Text = Form1.Refval[3].ToString();
            humiLtxB.Text = Form1.Refval[4].ToString();
            humiHtxB.Text = Form1.Refval[5].ToString();         
            vocLtxB.Text = Form1.Refval[6].ToString();
            vocHtxB.Text = Form1.Refval[7].ToString();
            h2sLtxB.Text = Form1.Refval[8].ToString();
            h2sHtxB.Text = Form1.Refval[9].ToString();
            pm25LtxB.Text = Form1.Refval[10].ToString();
            pm25HtxB.Text = Form1.Refval[11].ToString();
            
            tempLtxB.Focus(); // 获得焦点
            tempLtxB.Select(tempLtxB.TextLength, 0); // 光标移动到最后
        }


        private void button1_Click(object sender, EventArgs e)
        {
            #region 数据库数据写入
            string conStr = MysqlCon;
            conn = new MySqlConnection(conStr);
            try
            {
                conn.Open();
                string query = "update environment_ref SET an_ref = '" + anLtxB.Text + "-" + anHtxB.Text + "', temp_ref = '" + tempLtxB.Text + "-" + tempHtxB.Text + "',humi_ref = '" + humiLtxB.Text + "-" + humiHtxB.Text + "',h2s_ref = '" + h2sLtxB.Text + "-" + h2sHtxB.Text + "', voc_ref = '" + vocLtxB.Text + "-" + vocHtxB.Text + "',pm25_ref = '" + pm25LtxB.Text + "-" + pm25HtxB.Text + "'where id=1";
                MySqlCommand mycmd = new MySqlCommand(query, conn);
                mycmd.ExecuteNonQuery();
                setRefVal();
                MessageBox.Show("设置成功");
                this.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("设置失败");             
            }
            conn.Close();
            #endregion
        }

        private void setRefVal()
        {
            Form1.Refval[0] = double.Parse(anLtxB.Text);
            Form1.Refval[1] = double.Parse(anHtxB.Text);
            Form1.Refval[2] = double.Parse(tempLtxB.Text);
            Form1.Refval[3] = double.Parse(tempHtxB.Text);
            Form1.Refval[4] = double.Parse(humiLtxB.Text);
            Form1.Refval[5] = double.Parse(humiHtxB.Text);
            Form1.Refval[6] = double.Parse(h2sLtxB.Text);
            Form1.Refval[7] = double.Parse(h2sHtxB.Text);
            Form1.Refval[8] = double.Parse(vocLtxB.Text);
            Form1.Refval[9] = double.Parse(vocHtxB.Text);
            Form1.Refval[10] = double.Parse(pm25LtxB.Text);
            Form1.Refval[11] = double.Parse(pm25HtxB.Text);
        }
    }
}
